﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;

namespace Rose.Helper
{
    public class ArticleHelpModel
    {
        private Database_RoseEntities db = new Database_RoseEntities();
        private bool? ChangeIntToBool(int isOnlyForFriend)
        {
            switch (isOnlyForFriend)
            {
                case 0: return null;
                case 1: return true;
                default: return false;
            }
        }
        private int ChangeBoolToInt(bool? isOnlyForFriend)
        {
            switch (isOnlyForFriend)
            {
                case null: return 0;
                case true: return 1;
                default: return 2;
            }
        }
        public bool AddArticleToDatabase(ArticleModel model, string selfId)
        {
            try
            {

                Article addModel = new Article()
                {
                    Id = Guid.NewGuid(),
                    CreateTime = DateTime.Now,
                    CommentCount = 0,
                    Hit = 0,
                    ShareCount = 0,
                    GoodCount = 0,
                    IsOnlyForFriend = ChangeIntToBool(model.IsOnlyForFriend),
                    Content = model.Content,
                    Title = model.Title,
                    UserId = selfId

                };
                db.AddToArticles(addModel);
                //新鲜事更新
                UserInfo selfInfo = db.UserInfos.Where(c => c.Id == selfId).FirstOrDefault();
                if (selfInfo == null)
                    throw new ArgumentNullException();
                selfInfo.Score += int.Parse(Rose.ViewRes.Defaultresx.ArticleAddScore);
                new NewTrendHelpModel().AddArticleTrend(addModel);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
            ///没有进行任何编码
        }

        public void UpdateArticle(ArticleModel model, string selfId)
        {
            try
            {

                Article article = db.Articles.Where(c => c.Id == new Guid(model.Id) && c.UserId == selfId).FirstOrDefault();
                if (article == null)
                    return;
                article.Content = model.Content;
                article.Title = model.Title;
                db.SaveChanges();


            }
            catch
            {
                throw;
            }

        }

        public bool RemoveArticleFromDatabase(string id, string selfId)
        {
            try
            {
                Guid guidId = new Guid(id);
                Article deleteModel = db.Articles.Where(c => c.Id == new Guid(id) && c.UserId == selfId).FirstOrDefault();
                if (deleteModel == null)
                    return true;
                db.Articles.DeleteObject(deleteModel);
                db.SaveChanges();
                return true;
            }
            catch
            { return false; }

        }



        #region List
        private ArticleAbstractModel ChangeArticleToAbstractModel(Article article)
        {
            //string returnContent = article.Content.Length > 100 ? article.Content.Substring(0, 100) + "……" : article.Content;
            //string returnContent=HttpContext.Current.Server.HtmlDecode(article.Content);
            string decodeContent = article.DecodeContent;

            string returnContent = decodeContent.Trim().Length > 100 ? decodeContent.Trim().Substring(0, 100) + "……" : decodeContent.Trim();
            UserInfo selfInfo = new CheckHelpModel().CheckUserInfo(article.UserId);
            ArticleAbstractModel addModel = new ArticleAbstractModel()
            {
                Content = returnContent,
                ShareCount = article.ShareCount,
                CommentCount = article.CommentCount,
                CreateTime = article.CreateTime.ToString(),
                Hit = article.Hit,
                Title = article.EncodeTitle,
                ImagePath = selfInfo.ImagePath,
                UserId = article.UserId,
                Id = article.Id.ToString()

            };
            return addModel;
        }

        private List<ArticleAbstractModel> ChangeArticleToArticleAbstractModel(List<Article> articlelist)
        {
            List<ArticleAbstractModel> returnModel = new List<ArticleAbstractModel>();
            foreach (var article in articlelist)
            {

                returnModel.Add(ChangeArticleToAbstractModel(article));
            }
            return returnModel;
        }

        private List<Article> GetArticleList(string selfId, bool? isOnlyForFriend, int currentpage, int pagesize)
        {
            ///null ---self
            ///true-----friend
            ///false----vistor
            if (isOnlyForFriend == null)
            {
                return (from item in db.Articles
                        where item.UserId == selfId
                        select item
                         ).ToList();
            }
            else if (isOnlyForFriend == true)
            {
                return (from item in db.Articles
                        where item.UserId == selfId && item.IsOnlyForFriend != null
                        select item
                        ).ToList();
            }
            else
                return (from item in db.Articles
                        where item.UserId == selfId && item.IsOnlyForFriend == false
                        select item
                        ).ToList();

        }

        public List<ArticleAbstractModel> GetArticleAbstractModelList(string selfId, bool? isOnlyForFriend, int currentpage, int pagesize)
        {
            return ChangeArticleToArticleAbstractModel(GetArticleList(selfId, isOnlyForFriend, currentpage, pagesize));
        }

        public int GetArticleCount(string selfId, bool? isOnlyForFriend)
        {
            return GetArticleList(selfId, isOnlyForFriend,1,int.MaxValue).Count();
        }

        public ArticleModel GetArticleModel(string id, bool? isOnlyForFriend)
        {
            //null 表示自己
            //true 表示朋友
            //false 表示任何人
            Article article;
            switch (isOnlyForFriend)
            {
                case null:
                    article = db.Articles.Where(c => c.Id == new Guid(id)).FirstOrDefault();
                    break;
                case true:
                    article = db.Articles.Where(c => c.Id == new Guid(id) && c.IsOnlyForFriend != null).FirstOrDefault();
                    break;
                default:
                    article = db.Articles.Where(c => c.Id == new Guid(id) && c.IsOnlyForFriend == false).FirstOrDefault();
                    break;

            }

            UserInfo userInfo = new CheckHelpModel().CheckUserInfo(article.UserId);
            if (article != null)
            {
                return new ArticleModel()

                {
                    Author = userInfo.Name,
                    CommentCount = article.CommentCount,
                    Content = article.DecodeContent,
                    Title = article.EncodeTitle,
                    CreateTime = article.CreateTime.ToString(),
                    Hit = article.Hit,
                    Id = article.Id.ToString(),
                    ShareCount = article.ShareCount,
                    IsOnlyForFriend = ChangeBoolToInt(article.IsOnlyForFriend)

                };
            }


            return new ArticleModel();

        }

        

        #endregion



    }

}